{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "###########调包\n",
    "import os\n",
    "import numpy as np\n",
    "import pandas as pd\n",
    "from datetime import *\n",
    "import time\n",
    "import pickle"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "############数据文件文件路径\n",
    "train_dir = '../../contest/train/'\n",
    "B_dir = '../../contest/B榜/'\n",
    "train_pickle_dir = './pickle/train/'\n",
    "B_pickle_dir = './pickle/B/'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "def count_notzero(series_x):\n",
    "    mode = series_x[(series_x > 0)]\n",
    "    return mode.count()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "def 加工法人流水1():\n",
    "    res = []\n",
    "    for data_dir,pickle_dir in [(train_dir,train_pickle_dir),(B_dir,B_pickle_dir)]:\n",
    "        if data_dir==train_dir:\n",
    "            法人流水_T0 = pd.read_csv(os.path.join(data_dir,'XW_RPSTV_TR_DTAL.csv'))    \n",
    "            法人流水_T0.columns = ['法定代表人客户ID','交易日期','交易金额']\n",
    "        else:\n",
    "            法人流水_T0 = pd.read_csv(os.path.join(data_dir,'XW_RPSTV_TR_DTAL_B.csv'))    \n",
    "            法人流水_T0.columns = ['法定代表人客户ID','交易日期','交易金额']\n",
    "\n",
    "        法人流水_T0['交易金额'] = pow((法人流水_T0['交易金额'])/3.12,3).round(2)\n",
    "\n",
    "        法人流水_T0['交易日期'] = 法人流水_T0['交易日期'].astype('str')\n",
    "        法人流水_T0['交易日期'] = 法人流水_T0['交易日期'].astype('datetime64[ns]')\n",
    "        法人流水_T0['交易日期']=pd.to_datetime(法人流水_T0['交易日期'])+pd.DateOffset(days=11886)\n",
    "        法人流水_T0.sort_values(['法定代表人客户ID','交易日期'],inplace=True,ascending=True)\n",
    "        法人流水_T0['交易日期'] = 法人流水_T0['交易日期'].astype('str')\n",
    "        \n",
    "        法人流水_T0['流入金额'] =np.where((法人流水_T0['交易金额']>=0),法人流水_T0['交易金额'],0)\n",
    "        法人流水_T0['流出金额'] =np.where((法人流水_T0['交易金额']<=0),法人流水_T0['交易金额']*-1,0)\n",
    "\n",
    "        法人流水_单日汇总_T1=法人流水_T0.groupby(['法定代表人客户ID','交易日期']).agg({\\\n",
    "\t\t'流入金额':['sum',count_notzero],'流出金额':['sum',count_notzero]})\n",
    "        法人流水_单日汇总_T1.reset_index(inplace=True)\n",
    "        法人流水_单日汇总_T1.columns = ['法定代表人客户ID','交易日期','流入金额','流入笔数','流出金额','流出笔数']\n",
    "\n",
    "        法人流水_单日汇总_T1['最大日期']='2021-03-31'\n",
    "        法人流水_单日汇总_T1['最大日期'] = 法人流水_单日汇总_T1['最大日期'].astype('datetime64[ns]')\n",
    "        法人流水_单日汇总_T1['交易日期'] = 法人流水_单日汇总_T1['交易日期'].astype('datetime64[ns]')\n",
    "        法人流水_单日汇总_T1['交易离最大日期天']= 法人流水_单日汇总_T1.apply(lambda x:(x['最大日期']-x['交易日期']).days, axis=1)\n",
    "        法人流水_单日汇总_T1.drop(['最大日期'],axis=1,inplace=True)\n",
    "\n",
    "        pickle.dump(法人流水_单日汇总_T1, open(pickle_dir+'法人流水_临时表.p', 'wb'))\n",
    "        res.append(法人流水_单日汇总_T1)\n",
    "    return "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "def 加工法人流水2():\n",
    "    res = []\n",
    "    for data_dir,pickle_dir in [(train_dir,train_pickle_dir),(B_dir,B_pickle_dir)]:\n",
    "        if data_dir==train_dir:\n",
    "            法人流水_T0 = pickle.load(open(pickle_dir+'法人流水_临时表.p', 'rb'))\n",
    "        else:\n",
    "            法人流水_T0 = pickle.load(open(pickle_dir+'法人流水_临时表.p', 'rb'))\n",
    "\n",
    "        法人流水_T0['交易月']=pd.DatetimeIndex(法人流水_T0['交易日期']).month\n",
    "\n",
    "        法人流水_按月汇总_T1=法人流水_T0.groupby(['法定代表人客户ID','交易月']).agg({\\\n",
    "\t\t'流入金额':['sum'],'流入笔数':['sum'],'流出金额':['sum'],'流出笔数':['sum'],'交易日期':['count']})\n",
    "        法人流水_按月汇总_T1.reset_index(inplace=True)\n",
    "        法人流水_按月汇总_T1.columns = ['法定代表人客户ID','交易月','月流入金额','月流入笔数','月流出金额','月流出笔数','月交易天数']\n",
    "\n",
    "        法人流水_汇总_T1=法人流水_按月汇总_T1.groupby(['法定代表人客户ID']).agg({\\\n",
    "\t\t'月流入金额':['sum'],'月流入笔数':['sum'],'月流出金额':['sum'],'月流出笔数':['sum'],'月交易天数':['sum']})\n",
    "        法人流水_汇总_T1.reset_index(inplace=True)\n",
    "        法人流水_汇总_T1.columns = ['法定代表人客户ID','法人总流入金额','法人总流入笔数','法人总流出金额','法人总流出笔数','法人总交易天数']\n",
    "\n",
    "        法人流水_汇总_T1['法人总净流']=法人流水_汇总_T1['法人总流入金额']-法人流水_汇总_T1['法人总流出金额']\n",
    "        法人流水_汇总_T1['法人总金额']=法人流水_汇总_T1['法人总流入金额']+法人流水_汇总_T1['法人总流出金额']\n",
    "\n",
    "        三月汇总_T0= 法人流水_按月汇总_T1.loc[法人流水_按月汇总_T1['交易月'] == 3]\n",
    "        三月汇总_T0.drop(['交易月'],axis=1,inplace=True)\n",
    "        三月汇总_T0.columns = ['法定代表人客户ID','法人3月流入金额','法人3月流入笔数','法人3月流出金额','法人3月流出笔数','法人3月交易天数']\n",
    "        三月汇总_T0['法人3月净流']=三月汇总_T0['法人3月流入金额']-三月汇总_T0['法人3月流出金额']\n",
    "        三月汇总_T0['法人3月总金额']=三月汇总_T0['法人3月流入金额']+三月汇总_T0['法人3月流出金额']\n",
    "\n",
    "        if data_dir==train_dir:\n",
    "            目标客户列表 = pd.read_csv(os.path.join(data_dir,'XW_TARGET.csv'))\n",
    "            目标客户列表.columns = ['借款合同编号','客户ID','纳税人识别号','法定代表人客户ID','违约标记']\n",
    "            目标客户列表.drop(['违约标记'],axis=1,inplace=True)\n",
    "        else:\n",
    "            目标客户列表 = pd.read_csv(os.path.join(data_dir,'XW_TARGET_B.csv'))\n",
    "            目标客户列表.columns = ['借款合同编号','客户ID','纳税人识别号','法定代表人客户ID']\n",
    "\n",
    "        法人流水特征=目标客户列表.merge(法人流水_汇总_T1,on=['法定代表人客户ID'],how='left')\n",
    "        法人流水特征=法人流水特征.merge(三月汇总_T0,on=['法定代表人客户ID'],how='left')\n",
    "        法人流水特征.drop(['借款合同编号','纳税人识别号','法定代表人客户ID'],axis=1,inplace=True)\n",
    "\n",
    "        pickle.dump(法人流水特征, open(pickle_dir+'法人流水特征.p', 'wb'))\n",
    "        res.append(法人流水特征)\n",
    "    return res[0],res[1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "def 加工法人流水_补充():\n",
    "    res = []\n",
    "    for data_dir,pickle_dir in [(train_dir,train_pickle_dir),(B_dir,B_pickle_dir)]:\n",
    "        if data_dir==train_dir:\n",
    "            法人流水_T1 = pickle.load(open(pickle_dir+'法人流水_临时表.p', 'rb'))\n",
    "        else:\n",
    "            法人流水_T1 = pickle.load(open(pickle_dir+'法人流水_临时表.p', 'rb'))\n",
    "\t\t\t\n",
    "        法人流水_T2=法人流水_T1.groupby(['法定代表人客户ID']).agg({'交易日期':['max']})\n",
    "        法人流水_T2.reset_index(inplace=True)\n",
    "        法人流水_T2.columns = ['法定代表人客户ID','交易日期']\n",
    "        \n",
    "        法人流水_T3=法人流水_T1.merge(法人流水_T2,on=['法定代表人客户ID','交易日期'],how='inner')\n",
    "        法人流水_T3['最后一天总净流']=法人流水_T3['流入金额']-法人流水_T3['流出金额']\n",
    "        法人流水_T3['最后一天总金额']=法人流水_T3['流入金额']+法人流水_T3['流出金额']\n",
    "\n",
    "        法人流水_T3.drop(['交易日期','流入金额','流出金额'],axis=1,inplace=True)\n",
    "\n",
    "        法人流水_T3.columns = ['法定代表人客户ID','法人最后一天流入笔数','法人最后一天流出笔数','法人最后一天日期差','法人最后一天总净流','法人最后一天总金额']\n",
    "\n",
    "        if data_dir==train_dir:\n",
    "            目标客户列表 = pd.read_csv(os.path.join(data_dir,'XW_TARGET.csv'))\n",
    "            目标客户列表.columns = ['借款合同编号','客户ID','纳税人识别号','法定代表人客户ID','违约标记']\n",
    "            目标客户列表.drop(['违约标记'],axis=1,inplace=True)\n",
    "        else:\n",
    "            目标客户列表 = pd.read_csv(os.path.join(data_dir,'XW_TARGET_B.csv'))\n",
    "            目标客户列表.columns = ['借款合同编号','客户ID','纳税人识别号','法定代表人客户ID']\n",
    "\n",
    "        法人流水补充=目标客户列表.merge(法人流水_T3,on=['法定代表人客户ID'],how='left')\n",
    "        法人流水补充.drop(['借款合同编号','纳税人识别号','法定代表人客户ID'],axis=1,inplace=True)\n",
    "\n",
    "        pickle.dump(法人流水补充, open(pickle_dir+'法人流水补充.p', 'wb'))\n",
    "        res.append(法人流水补充)\n",
    "    return res[0],res[1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "加工法人流水1()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "<ipython-input-5-b91dd72a190c>:25: SettingWithCopyWarning: \n",
      "A value is trying to be set on a copy of a slice from a DataFrame\n",
      "\n",
      "See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy\n",
      "  三月汇总_T0.drop(['交易月'],axis=1,inplace=True)\n",
      "<ipython-input-5-b91dd72a190c>:27: SettingWithCopyWarning: \n",
      "A value is trying to be set on a copy of a slice from a DataFrame.\n",
      "Try using .loc[row_indexer,col_indexer] = value instead\n",
      "\n",
      "See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy\n",
      "  三月汇总_T0['法人3月净流']=三月汇总_T0['法人3月流入金额']-三月汇总_T0['法人3月流出金额']\n",
      "<ipython-input-5-b91dd72a190c>:28: SettingWithCopyWarning: \n",
      "A value is trying to be set on a copy of a slice from a DataFrame.\n",
      "Try using .loc[row_indexer,col_indexer] = value instead\n",
      "\n",
      "See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy\n",
      "  三月汇总_T0['法人3月总金额']=三月汇总_T0['法人3月流入金额']+三月汇总_T0['法人3月流出金额']\n",
      "<ipython-input-5-b91dd72a190c>:25: SettingWithCopyWarning: \n",
      "A value is trying to be set on a copy of a slice from a DataFrame\n",
      "\n",
      "See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy\n",
      "  三月汇总_T0.drop(['交易月'],axis=1,inplace=True)\n",
      "<ipython-input-5-b91dd72a190c>:27: SettingWithCopyWarning: \n",
      "A value is trying to be set on a copy of a slice from a DataFrame.\n",
      "Try using .loc[row_indexer,col_indexer] = value instead\n",
      "\n",
      "See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy\n",
      "  三月汇总_T0['法人3月净流']=三月汇总_T0['法人3月流入金额']-三月汇总_T0['法人3月流出金额']\n",
      "<ipython-input-5-b91dd72a190c>:28: SettingWithCopyWarning: \n",
      "A value is trying to be set on a copy of a slice from a DataFrame.\n",
      "Try using .loc[row_indexer,col_indexer] = value instead\n",
      "\n",
      "See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy\n",
      "  三月汇总_T0['法人3月总金额']=三月汇总_T0['法人3月流入金额']+三月汇总_T0['法人3月流出金额']\n"
     ]
    }
   ],
   "source": [
    "法人流水训练集,法人流水测试集=加工法人流水2()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(50000, 15)"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "法人流水训练集.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(5939, 15)"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "法人流水测试集.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [],
   "source": [
    "法人流水补充训练集,法人流水补充测试集=加工法人流水_补充()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(50000, 6)"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "法人流水补充训练集.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(5939, 6)"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "法人流水补充测试集.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [],
   "source": [
    "def 加工法人流水_补充2():\n",
    "    res = []\n",
    "    for data_dir,pickle_dir in [(train_dir,train_pickle_dir),(B_dir,B_pickle_dir)]:\t\t\n",
    "        if data_dir==train_dir:\n",
    "            法定代表人交易信息表 = pd.read_csv(os.path.join(data_dir,'XW_RPSTV_TR_DTAL.csv'))    \n",
    "        else:\n",
    "            法定代表人交易信息表 = pd.read_csv(os.path.join(data_dir,'XW_RPSTV_TR_DTAL_B.csv'))       \n",
    "        法定代表人交易信息表.columns = ['法定代表人客户ID','交易日期','交易金额']\n",
    "\n",
    "        法定代表人交易信息表['交易金额'] = pow((法定代表人交易信息表['交易金额'])/3.12,3).round(2)\n",
    "        法定代表人交易信息表['交易日期'] = 法定代表人交易信息表['交易日期'].astype('str')\n",
    "        法定代表人交易信息表['交易日期'] = 法定代表人交易信息表['交易日期'].astype('datetime64[ns]')\n",
    "        法定代表人交易信息表['交易日期']=pd.to_datetime(法定代表人交易信息表['交易日期'])+pd.DateOffset(days=11886)\n",
    "\n",
    "        #新增绝对值相关std\n",
    "        法人交易T0 = 法定代表人交易信息表.copy()\n",
    "        法人交易T0['法人金额绝对值'] =np.where((法定代表人交易信息表['交易金额']>=0),法定代表人交易信息表['交易金额'],法定代表人交易信息表['交易金额']*-1)\n",
    "        法人交易T0 = 法人交易T0.groupby(['法定代表人客户ID']).agg({'法人金额绝对值':['max','min','mean','std']})\n",
    "        法人交易T0.reset_index(inplace = True)\n",
    "        法人交易T0.columns=['法定代表人客户ID','法人交易绝对值最高金额','法人交易绝对值最低金额','法人交易绝对值_mean','法人交易绝对值_std']\n",
    "        \n",
    "        法人交易T1 = 法定代表人交易信息表.groupby(['法定代表人客户ID']).agg({'交易金额':['sum','max','min','count','mean','std']})\n",
    "        法人交易T1.reset_index(inplace = True)\n",
    "        法人交易T1.columns=['法定代表人客户ID','法人交易金额汇总','法人交易最高金额','法人交易最低金额','法人交易笔数','法人交易_mean','法人交易_std']\n",
    "        \n",
    "        法人交易T2 = 法定代表人交易信息表.drop(labels = ['交易金额'],axis=1)\n",
    "        法人交易T2 = 法人交易T2.drop_duplicates()\n",
    "        print(法人交易T2.shape)\n",
    "        法人交易T3 = 法人交易T2.groupby(['法定代表人客户ID']).agg({'交易日期':['count','max','min']})\n",
    "        法人交易T3.reset_index(inplace = True)\n",
    "        法人交易T3.columns=['法定代表人客户ID','法人交易天数','法人交易日期max','法人交易日期min']\n",
    "        法人交易T3['法人时间跨度']= 法人交易T3.apply(lambda x:(x['法人交易日期max']-x['法人交易日期min']).days, axis=1)\n",
    "        \n",
    "        法人交易T4=法定代表人交易信息表.loc[法定代表人交易信息表['交易金额']>=0]\n",
    "        法人交易T4=法人交易T4.groupby(['法定代表人客户ID']).agg({'交易金额':['sum','count','max','min','mean','std']})\n",
    "        法人交易T4.reset_index(inplace=True)\n",
    "        法人交易T4.columns=['法定代表人客户ID','法人流入总额','法人流入笔数','法人流入max','法人流入min','法人流入mean','法人流入std']\n",
    "        \n",
    "        法人交易T5=法定代表人交易信息表.loc[法定代表人交易信息表['交易金额']<0]\n",
    "        法人交易T5=法人交易T5.groupby(['法定代表人客户ID']).agg({'交易金额':['sum','count','max','min','mean','std']})\n",
    "        法人交易T5.reset_index(inplace=True)\n",
    "        法人交易T5.columns=['法定代表人客户ID','法人流出总额','法人流出笔数','法人流出max','法人流出min','法人流出mean','法人流出std']\n",
    "        \n",
    "        法人交易T6 = 法人交易T1.merge(法人交易T3,on='法定代表人客户ID',how='left')\n",
    "        法人交易T7 = 法人交易T6.merge(法人交易T4,on='法定代表人客户ID',how='left')\n",
    "        法人交易T8 = 法人交易T7.merge(法人交易T5,on='法定代表人客户ID',how='left')\n",
    "    \n",
    "        if data_dir==train_dir:\n",
    "            目标客户表 = pd.read_csv(os.path.join(data_dir,'XW_TARGET.csv'))\n",
    "            目标客户表.columns = ['借款合同编号','客户ID','纳税人识别号','法定代表人客户ID','违约标记']\n",
    "            目标客户表.drop(['违约标记'],axis=1,inplace=True)\n",
    "        else:\n",
    "            目标客户表 = pd.read_csv(os.path.join(data_dir,'XW_TARGET_B.csv'))\n",
    "            目标客户表.columns = ['借款合同编号','客户ID','纳税人识别号','法定代表人客户ID']\n",
    "\t\n",
    "        法人交易宽表 = 目标客户表.merge(法人交易T8,on='法定代表人客户ID',how='left')\n",
    "        法人交易宽表 = 法人交易宽表.drop(labels = ['纳税人识别号','法定代表人客户ID','借款合同编号','法人交易日期max','法人交易日期min'],axis=1)\n",
    "        for a in list(法人交易宽表.select_dtypes(include=['float']).columns.values):\n",
    "            法人交易宽表[a]=法人交易宽表[a].round(2)\n",
    "        print(法人交易宽表['法人交易天数'].max())\n",
    "        pickle.dump(法人交易宽表, open(pickle_dir+'法人交易宽表DJY.p', 'wb'))\n",
    "\t\t\n",
    "        res.append(法人交易宽表)\n",
    "    return res[0],res[1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(1320301, 2)\n",
      "90.0\n",
      "(156820, 2)\n",
      "90.0\n"
     ]
    }
   ],
   "source": [
    "法人流水补充2训练集,法人流水补充2测试集 = 加工法人流水_补充2()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(50000, 21)"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "法人流水补充2训练集.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(5939, 21)"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "法人流水补充2测试集.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [],
   "source": [
    "def 加工法定代表人交易信息特征补充():\n",
    "    res = [] \n",
    "    for data_dir,pickle_dir in [(train_dir,train_pickle_dir),(B_dir,B_pickle_dir)]:\n",
    "        if data_dir==train_dir:\n",
    "            RPSTV_TR_DTAL_t0 = pd.read_csv(os.path.join(data_dir,'XW_RPSTV_TR_DTAL.csv')) \n",
    "        else:\n",
    "            RPSTV_TR_DTAL_t0 = pd.read_csv(os.path.join(data_dir,'XW_RPSTV_TR_DTAL_B.csv'))\n",
    "\n",
    "        RPSTV_TR_DTAL_t0.columns = ['法定代表人客户ID','数据日期','交易金额']\n",
    "        RPSTV_TR_DTAL_t0.sort_values(['法定代表人客户ID','数据日期'])\n",
    "        \n",
    "\n",
    "        RPSTV_TR_DTAL_t0['数据日期']= RPSTV_TR_DTAL_t0['数据日期'].astype('str')\n",
    "        RPSTV_TR_DTAL_t0['数据日期'] = RPSTV_TR_DTAL_t0['数据日期'].astype('datetime64[ns]')\n",
    "        RPSTV_TR_DTAL_t0['数据日期']=pd.to_datetime(RPSTV_TR_DTAL_t0['数据日期'])+pd.DateOffset(days=11886)\n",
    "        RPSTV_TR_DTAL_t0['交易金额'] = pow((RPSTV_TR_DTAL_t0['交易金额'])/3.12,3)\n",
    "\n",
    "        RPSTV_TR_DTAL_t0['交易金额绝对值'] = RPSTV_TR_DTAL_t0['交易金额'].apply(lambda x:x if x>0 else x*(-1))\n",
    "\n",
    "        RPSTV_TR_DTAL_t6=RPSTV_TR_DTAL_t0.groupby(['法定代表人客户ID']).agg({'交易金额':['sum','count'],'交易金额绝对值':['sum']})\n",
    "        RPSTV_TR_DTAL_t6.reset_index(inplace=True)\n",
    "        RPSTV_TR_DTAL_t6.columns = ['法定代表人客户ID','法定代表人交易金额汇总','法定代表人总交易笔数','交易金额绝对值汇总']\n",
    "        \n",
    "        #静怡 只按日期汇总 std min 流出的std min，sum/count=mean(三个只留一个)\n",
    "        \n",
    "#         相关性：去掉    法定代表人资金净流入额\n",
    "        RPSTV_TR_DTAL_t3=RPSTV_TR_DTAL_t0[RPSTV_TR_DTAL_t0['交易金额']>0].groupby(['法定代表人客户ID']).agg({'交易金额':['sum','count']})\n",
    "        RPSTV_TR_DTAL_t3.reset_index(inplace=True)\n",
    "        RPSTV_TR_DTAL_t3.columns = ['法定代表人客户ID','法定代表人资金净流入额','法定代表人净流入交易笔数']\n",
    "\n",
    "        \n",
    "        RPSTV_TR_DTAL_t1=RPSTV_TR_DTAL_t0.groupby(['法定代表人客户ID','数据日期']).agg({'交易金额':['sum'],'交易金额绝对值':['sum']})\n",
    "        RPSTV_TR_DTAL_t1.reset_index(inplace=True)\n",
    "        RPSTV_TR_DTAL_t1.columns = ['法定代表人客户ID','数据日期' ,'日交易金额','日交易金额绝对值']\n",
    "\n",
    "        RPSTV_TR_DTAL_t2=RPSTV_TR_DTAL_t1.groupby(['法定代表人客户ID']).agg({'日交易金额':['max','min','std','mean','count'],\\\n",
    "                                                                      '日交易金额绝对值':['max','min','std','mean']})\n",
    "        RPSTV_TR_DTAL_t2.reset_index(inplace=True)\n",
    "        RPSTV_TR_DTAL_t2.columns = ['法定代表人客户ID','日交易金额_max','日交易金额_min','日交易金额_std','日交易金额_mean','日交易金额_count'\\\n",
    "                                   ,'日交易金额绝对值_max','日交易金额绝对值_min','日交易金额绝对值_std','日交易金额绝对值_mean']\n",
    " \n",
    "        \n",
    "        RPSTV_TR_DTAL_t4=RPSTV_TR_DTAL_t0[RPSTV_TR_DTAL_t0['交易金额']>0].groupby(['法定代表人客户ID','数据日期']).agg({'交易金额':['sum']})\n",
    "        RPSTV_TR_DTAL_t4.reset_index(inplace=True)\n",
    "        RPSTV_TR_DTAL_t4.columns = ['法定代表人客户ID','数据日期' ,'日交易金额净流入']\n",
    "    \n",
    "        RPSTV_TR_DTAL_t5=RPSTV_TR_DTAL_t4.groupby(['法定代表人客户ID']).agg({'日交易金额净流入':['max','min','std','mean','count']})\n",
    "        RPSTV_TR_DTAL_t5.reset_index(inplace=True)\n",
    "        RPSTV_TR_DTAL_t5.columns = ['法定代表人客户ID','日交易金额净流入_max','日交易金额净流入_min','日交易金额净流入_std','日交易金额净流入_mean'\\\n",
    "                                    ,'日交易金额净流入_count']        \n",
    "\n",
    "        if data_dir==train_dir:\n",
    "            目标客户列表 = pd.read_csv(os.path.join(data_dir,'XW_TARGET.csv'))\n",
    "            目标客户列表.columns = ['借款合同编号','客户ID','纳税人识别号','法定代表人客户ID','违约标记']\n",
    "            目标客户列表.drop(['违约标记'],axis=1,inplace=True)\n",
    "        else:\n",
    "            目标客户列表 = pd.read_csv(os.path.join(data_dir,'XW_TARGET_B.csv'))\n",
    "            目标客户列表.columns = ['借款合同编号','客户ID','纳税人识别号','法定代表人客户ID']\n",
    "\n",
    "        \n",
    "        法定代表人交易信息特征=目标客户列表.merge(RPSTV_TR_DTAL_t6,on=['法定代表人客户ID'],how='left')\n",
    "        法定代表人交易信息特征=法定代表人交易信息特征.merge(RPSTV_TR_DTAL_t3,on=['法定代表人客户ID'],how='left')\n",
    "        法定代表人交易信息特征=法定代表人交易信息特征.merge(RPSTV_TR_DTAL_t2,on=['法定代表人客户ID'],how='left')\n",
    "        法定代表人交易信息特征=法定代表人交易信息特征.merge(RPSTV_TR_DTAL_t5,on=['法定代表人客户ID'],how='left')\n",
    "     \n",
    "        法定代表人交易信息特征.fillna(0,inplace=True)\n",
    "        法定代表人交易信息特征.drop_duplicates(subset=None,keep='first',inplace=True)\n",
    "\n",
    "        法定代表人交易信息特征.drop(['借款合同编号','纳税人识别号','法定代表人客户ID'],axis=1,inplace=True)\n",
    "        pickle.dump(法定代表人交易信息特征, open(pickle_dir+'Z法定代表人交易信息特征.p', 'wb'))\n",
    "\n",
    "        res.append(法定代表人交易信息特征)\n",
    "    return res[0],res[1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [],
   "source": [
    "法人流水补充3训练集,法人流水补充3测试集 =加工法定代表人交易信息特征补充()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
